/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quizzempire.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;
import quizzempire.model.IModel;
import quizzempire.model.IQuizz;
import quizzempire.model.statemachine.State;

/**
 *
 * @author Victor
 */
public class EndGame extends javax.swing.JFrame implements Observer {

    /**
     * Creates new form EndGame
     */
    IModel model;
    IQuizz iquizz;
    
    public EndGame(IModel model) {
        initComponents();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        this.model = model;
        iquizz = model.getQuizzViewer();
        lbLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quizzempire/resources/gamerlogo.png")));
        initFields();
        model.addObserver(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbLogo = new javax.swing.JLabel();
        btMainMenu = new javax.swing.JButton();
        btPlayAgain = new javax.swing.JButton();
        lbEndMsg = new javax.swing.JLabel();
        lbScore = new javax.swing.JLabel();
        lbWinImg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(460, 390));
        setResizable(false);

        lbLogo.setText("LOGO");

        btMainMenu.setText("MAIN MENU");
        btMainMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMainMenuActionPerformed(evt);
            }
        });

        btPlayAgain.setText("PLAY AGAIN");
        btPlayAgain.setMaximumSize(new java.awt.Dimension(113, 23));
        btPlayAgain.setMinimumSize(new java.awt.Dimension(113, 23));
        btPlayAgain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPlayAgainActionPerformed(evt);
            }
        });

        lbEndMsg.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbEndMsg.setText("MENSAGEM");

        lbScore.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbScore.setText("PONTUAÇÃO");

        lbWinImg.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(btMainMenu)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 162, Short.MAX_VALUE)
                .addComponent(btPlayAgain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(74, 74, 74))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lbEndMsg, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbScore, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(lbWinImg, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbEndMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbWinImg, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbScore, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btMainMenu)
                    .addComponent(btPlayAgain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMainMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMainMenuActionPerformed
        model.restart();
    }//GEN-LAST:event_jMainMenuActionPerformed

    private void jPlayAgainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPlayAgainActionPerformed
        model.playAgain();
    }//GEN-LAST:event_jPlayAgainActionPerformed



    public void update(Observable o , Object arg){
    State state = model.getState(); 
              
        if (!(state instanceof quizzempire.model.statemachine.ShowResults))
        {
            model.deleteObserver(this);
            MainBuilder.changeScreen(this, model, state);
        }
    }
    
    private void initFields(){
        
        //lbLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/gamerlogo.png")));
        
        //faltam metodos para poder ir calcular a percentagem
        double percentagem = (double) model.getCorrectas() / iquizz.getNumQuestionsLevel()[model.getDifficulty()];
        
        if(percentagem>=0.6)lbEndMsg.setText("Congratulations!! Check your medal");
        else lbEndMsg.setText("You lost! No medal for you");
        
        if(percentagem>=0.6 && percentagem<=0.74)
            lbWinImg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quizzempire/resources/bronze-medal.png")));
                else if(percentagem>=.75 && percentagem<=0.90)
                    lbWinImg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quizzempire/resources/silver-medal.png")));
                else if(percentagem>=0.91 && percentagem<=1) 
                    lbWinImg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quizzempire/resources/gold-medal.png")));

        //falta implementar os getters seguintes para ir buscar os valores ao estado
        lbScore.setText("You scored "+model.getCorrectas()+" out of "+iquizz.getNumQuestionsLevel()[model.getDifficulty()]);
 
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btMainMenu;
    private javax.swing.JButton btPlayAgain;
    private javax.swing.JLabel lbEndMsg;
    private javax.swing.JLabel lbLogo;
    private javax.swing.JLabel lbScore;
    private javax.swing.JLabel lbWinImg;
    // End of variables declaration//GEN-END:variables
}
